package com.obsidian.free.slideshow;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class SlideshowConfigureLocal extends Activity implements OnClickListener {
   CheckBox randomize;
   EditText updateInterval;
   Button back, next;
   int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID, size;
   boolean edit;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.localpage);
      
      size = getIntent().getIntExtra("size", SlideshowUpdateService.SIZE_SMALL);
      edit = getIntent().getBooleanExtra("edit", false);
      appWidgetId = getIntent().getIntExtra("appWidgetId", appWidgetId);
      if(appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
         setResult(RESULT_CANCELED);
         Log.d("Slideshow", "ConfigLocal failed. Invalid WidgetID" + appWidgetId);
         finish();
         return;
      }
      
      randomize = (CheckBox)findViewById(R.id.CommonRandomCheck);
      updateInterval = (EditText)findViewById(R.id.CommonUpdateEdit);
      back = (Button)findViewById(R.id.LocalPageBackButton);
      next = (Button)findViewById(R.id.LocalPageNextButton);
      
      back.setOnClickListener(this);
      next.setOnClickListener(this);
      
      if(edit) {
         ImageDatabase db = new ImageDatabase(this);
         Bundle widget = db.getWidget(appWidgetId);
         randomize.setChecked(widget.getInt(ImageDatabase.WIDGETS_ORDER) == 1);
         updateInterval.setText(new Integer(widget.getInt(ImageDatabase.WIDGETS_PERIOD)).toString());
      }
   }

   public void onClick(View v) {
      switch(v.getId()) {
         case R.id.LocalPageBackButton:
            setResult(RESULT_CANCELED);
            finish();
            break;
         case R.id.LocalPageNextButton:
            if(updateInterval.getText().length() < 1) {
               Toast toast = Toast.makeText(this, getText(R.string.config_common_update_missing), 3);
               toast.show();
               return;
            }
            ImageDatabase db = new ImageDatabase(this);
            db.updateWidget(appWidgetId,
                  new Integer(updateInterval.getText().toString()).intValue(),
                  (randomize.isChecked() ? 1 : 0));
            db.close();
            /*Intent intent = new Intent(this, SlideshowConfigureImages.class);
            intent.putExtra("appWidgetId", appWidgetId);
            intent.putExtra("type", SlideshowConfigure.TYPE_LOCAL);
            intent.putExtra("size", size);
            startActivityForResult(intent, SlideshowConfigure.REQUEST_IMAGES);*/
            setResult(RESULT_OK);
            finish();
            break;
      }
   }
   
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if(resultCode == RESULT_OK) {
         setResult(RESULT_OK);
         finish();
      }
   }
}
